﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestByWire.Test.EmulatedTutorials
{
    [TestClass]
    [UseStub(typeof(IEmployeStorage), typeof(MyStorage))]
    public class ControllingDependencies : TestTarget<SalaryManagerHardCoupled>
    {
        [TestMethod]
        public void DependencyIsReplacedWithStub()
        {
            When.Calling.EmptyCall();

            Then<IEmployeStorage>.FulFills(x => x is MyStorage);
        }

        [TestMethod]
        [UseStub(typeof(IEmployeStorage), typeof(MyStorage2))]
        public void LocalDependencyDefinitionsOverrideGlobalDefinitions()
        {
            When.Calling.EmptyCall();

            Then<IEmployeStorage>.FulFills(x => x is MyStorage2);
        }

        [TestMethod]
        [UseStubLateBound("_taxCalculator")]
        public void UseLateboundStubDirectlyOnField()
        {
            When.Calling.EmptyCall();

            Then<ITaxationCalulator>.FulFills(x => x.GetType() != typeof(MyHardCoupling));
        }

        [TestMethod]
        [UseStubLateBound("_taxCalculator", Register.OnName)]
        public void LateBoundStubsCanBeRegisteredOnName()
        {
            When.Calling.EmptyCall();

            Then<ITaxationCalulator>.FulFills("_taxCalculator", x => x.GetType() != typeof(MyHardCoupling));
        }

    }

    public class MyStorage2 : MyStorage { }

    public class MyStorage : IEmployeStorage
    {
        public EmployeInformation RetrieveCurrentSalery(string employeeId)
        {
            return new EmployeInformation();
        }

        public void UpdateSalery(EmployeInformation employeInformation, double newSalery)
        {

        }
    }

    public class SalaryManagerHardCoupled : SalaryManager
    {
        public SalaryManagerHardCoupled(IEmployeStorage storage, ISaleryIndex saleryIndex, IEventRaiser eventRaiser)
            : base(storage, saleryIndex, new MyHardCoupling(), eventRaiser)
        {
        }

        public void EmptyCall()
        {}
    }

    public class MyHardCoupling : ITaxationCalulator
    {
        public double CalculateTax(double saleryAdjustment)
        {
            return 1000000;
        }
    }
}
